Design and implement the
data structure “stack”. Write the program to simulate the stack operations,
implement all methods mentioned below. The program reads the sequence of
commands and executes the corresponding operation. After processing each
command the program must print one line of output. The possible
commands are:
·
push n – Add to the stack the number n
(value n is given after the command).
Print
ok.
·
pop – Remove the last element
from the stack. Print the value of this element.
·
back – Print the value of the
last element, not removing it from the stack.
·
size – Print the number of
elements in the stack.
·
clear – Clear the stack and
print ok.
·
exit – Print bye and
terminate.
It is guaranteed that the
set of input commands satisfies the following requirements: the maximum number
of elements in the stack at any time does not exceed 100, all commands pop and back are correct, that is, when executed the stack contains at
least one element.
Input. Each line contains a single command.
Output. For each command print on
a separate line the corresponding result.
Sample
input |
Sample
output |
push 2 push 3 push 5 back size pop size push 7 pop clear size exit |
ok ok ok 5 3 5 2 ok 7 ok 0 bye |
stack
In this problem you need
to simulate the stack.
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
stack<int> s;
char str[100];
int n;
int main(void)
{
while(scanf("%s",str))
{
if
(strcmp(str,"push") == 0)
{
scanf("%d",&n);
s.push(n);
puts("ok");
} else
if
(strcmp(str,"pop") == 0)
{
printf("%d\n",s.top());
s.pop();
} else
if
(strcmp(str,"back") == 0)
{
printf("%d\n",s.top());
} else
if (strcmp(str,"size") == 0)
{
printf("%d\n",s.size());
} else
if
(strcmp(str,"clear") == 0)
{
while(!s.empty())
s.pop();
puts("ok");
} else
{
puts("bye");
break;
}
}
return 0;
}
Declare a stack.
stack<int> s;
Read a command str. Read the commands until the end of the file.
while(cin >> str)
{
if (str == "push")
{
Command push. ×èòàåì ÷èñëî n è çàíîñèì åãî â ñòåê. Âûâîäèì ñîîáùåíèå “ok”.
cin >> n;
s.push(n);
cout
<< "ok" << endl;
} else
if (str == "pop")
{
Command pop. Print the number at the top of the stack. Delete the top
element.
cout << s.top() << endl;
s.pop();
} else
if (str == "back")
{
Command top. Print the number at the top of the stack.
cout << s.top() << endl;
} else
if (str == "size")
{
Command size. Print the size of the stack.
cout << s.size() << endl;
} else
if (str == "clear")
{
Command clear. Delete the entire stack. Since C++ does not have a clear
method for the stack, we have to sequentially remove stack elements one by one.
while(!s.empty())
s.pop();
cout << "ok"
<< endl;
} else
{
Command exit. Print “bye” and terminate the program.
cout << "bye"
<< endl;
break;
}
}
return 0;
}
class Stack {
public:
Stack(int
_size);
~Stack();
void push(int v);
int pop();
int back();
int size();
void clear();
void exit();
private:
int* m;
int _size;
};
Stack::Stack(int _size = 110) {
m = new int[_size];
this->_size
= 0;
}
Stack::~Stack() {
delete[] m;
}
void Stack::push(int v) {
m[_size++] = v;
puts("ok");
}
int Stack::pop() {
return
m[--_size];
}
int Stack::back() {
return
m[_size-1];
}
int Stack::size() {
return _size;
}
void Stack::clear() {
_size = 0;
puts("ok");
}
void Stack::exit() {
puts("bye");
}
Main part of the program. Simulate
the stack.
Stack s;
while(scanf("%s",&str))
{
if
(strcmp(str,"push") == 0)
{
scanf("%d",&n);
s.push(n);
} else
if
(strcmp(str,"pop") == 0)
{
printf("%d\n",s.pop());
} else
if
(strcmp(str,"back") == 0)
{
printf("%d\n",s.back());
} else
if
(strcmp(str,"size") == 0)
{
printf("%d\n",s.size());
} else
if
(strcmp(str,"clear") == 0)
{
s.clear();
} else
{
s.exit();
break;
}
}
import java.util.*;
//import java.io.*;
public class Main
{
public static void main(String[] args) //throws IOException
{
Stack<Integer> s = new Stack<Integer>();
//Scanner con = new Scanner(new FileReader
("6122.in"));
Scanner con = new Scanner(System.in);
while(true)
{
String str = con.next();
if (str.equals("push"))
{
int n = con.nextInt();
s.push(n);
System.out.println("ok");
} else
if (str.equals("pop"))
{
System.out.println(s.pop());
} else
if (str.equals("back"))
{
System.out.println(s.peek());
} else
if (str.equals("size"))
{
System.out.println(s.size());
} else
if (str.equals("clear"))
{
s.clear();
System.out.println("ok");
} else
{
System.out.println("bye");
break;
}
}
con.close();
}
}
import java.util.*;
//import java.io.*;
class Stack
{
private int m[];
private int size;
public Stack(int size)
{
m = new int[size];
this.size = 0;
}
public void push(int v)
{
m[size++] = v;
System.out.println("ok");
}
public int pop()
{
return m[--size];
}
public int back()
{
return m[size-1];
}
public int size()
{
return size;
}
public void clear()
{
size = 0;
System.out.println("ok");
}
public void exit()
{
System.out.println("bye");
}
}
public class Main
{
public static void main(String[] args) //throws IOException
{
Stack s = new Stack(110);
//Scanner con = new Scanner(new FileReader
("6122.in"));
Scanner
con = new Scanner(System.in);
while(true)
{
String str = con.next();
if (str.equals("push"))
{
int n = con.nextInt();
s.push(n);
} else
if (str.equals("pop"))
{
System.out.println(s.pop());
} else
if (str.equals("back"))
{
System.out.println(s.back());
} else
if (str.equals("size"))
{
System.out.println(s.size());
} else
if (str.equals("clear"))
{
s.clear();
} else
{
s.exit();
break;
}
}
con.close();
}
}
import java.util.*;
//import java.io.*;
class Stack
{
private
LinkedList<Integer> m;
public Stack()
{
m = new LinkedList<Integer>();
}
public void push(int v)
{
m.add(v);
System.out.println("ok");
}
public int pop()
{
int val = m.getLast();
m.removeLast();
return val;
}
public int back()
{
return m.getLast();
}
public int size()
{
return m.size();
}
public void clear()
{
m.clear();
System.out.println("ok");
}
public void exit()
{
System.out.println("bye");
}
}
public class Main
{
public static void main(String[] args) //throws IOException
{
Stack s = new Stack();
//Scanner con = new Scanner(new FileReader
("6122.in"));
Scanner
con = new Scanner(System.in);
while(true)
{
String str = con.next();
if (str.equals("push"))
{
int n = con.nextInt();
s.push(n);
} else
if (str.equals("pop"))
{
System.out.println(s.pop());
} else
if (str.equals("back"))
{
System.out.println(s.back());
} else
if (str.equals("size"))
{
System.out.println(s.size());
} else
if (str.equals("clear"))
{
s.clear();
} else
{
s.exit();
break;
}
}
con.close();
}
}
import java.util.*;
class MyStack
{
protected Vector<Integer> v;
MyStack()
{
v = new
Vector<Integer>();
}
public void push(int x)
{
v.add(x);
}
public int pop()
{
int last = v.lastElement();
v.remove(v.size() - 1);
return last;
}
}
class NewStack extends MyStack
{
public void push(int x)
{
super.push(x);
System.out.println("ok");
}
/*
public int pop() // pop is totally the same as in superclass
{
return super.pop();
}
*/
public int back()
{
return v.get(v.size() - 1);
// v is visible from subclass, it is protected
}
public int size()
{
return v.size();
}
public void clear()
{
v.clear();
System.out.println("ok");
}
public void exit()
{
System.out.println("bye");
}
}
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
NewStack s = new NewStack();
while(true)
{
String str = con.next();
if (str.equals("push"))
{
int n = con.nextInt();
s.push(n);
} else
if (str.equals("pop"))
{
System.out.println(s.pop());
} else
if (str.equals("back"))
{
System.out.println(s.back());
} else
if (str.equals("size"))
{
System.out.println(s.size());
} else
if (str.equals("clear"))
{
s.clear();
} else
{
s.exit();
break;
}
}
con.close();
}
}
Python realization
import sys
stack = []
for str in sys.stdin:
lst = list(str.split())
if lst[0] == "push":
n = int(lst[1]);
stack.append(n);
print("ok")
elif lst[0] == "pop":
print(stack.pop())
elif lst[0] == "back":
print(stack[-1])
elif lst[0] == "size":
print(len(stack))
elif lst[0] == "clear":
stack.clear()
print("ok")
else:
print("bye")
break;